// BlogLikeController.java
package net.web.controller;

import net.web.common.utils.JSONUtil;
import net.web.common.utils.Result;
import net.web.service.BlogLikeService;
import net.web.common.framework.simplemvc.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("/api/like")
public class BlogLikeController {
    private BlogLikeService likeService = new BlogLikeService();

    @RequestMapping("/like.do")
    public void like(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        int userId = req.getParameter("user_id") != null ? Integer.parseInt(req.getParameter("user_id")) : Integer.parseInt(req.getParameter("userId"));
        boolean ok = likeService.likeBlog(blogId, userId);
        JSONUtil.printByJSON(resp, ok ? Result.success("点赞成功") : Result.fail("点赞失败"));
    }
    @RequestMapping("/unlike.do")
    public void unlike(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        int userId = req.getParameter("user_id") != null ? Integer.parseInt(req.getParameter("user_id")) : Integer.parseInt(req.getParameter("userId"));
        boolean ok = likeService.unlikeBlog(blogId, userId);
        JSONUtil.printByJSON(resp, ok ? Result.success("取消点赞成功") : Result.fail("取消点赞失败"));
    }
    @RequestMapping("/isLiked.do")
    public void isLiked(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        int userId = req.getParameter("user_id") != null ? Integer.parseInt(req.getParameter("user_id")) : Integer.parseInt(req.getParameter("userId"));
        JSONUtil.printByJSON(resp, Result.success(likeService.isLiked(blogId, userId)));
    }
    @RequestMapping("/likeCount.do")
    public void likeCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogId = req.getParameter("blog_id") != null ? Integer.parseInt(req.getParameter("blog_id")) : Integer.parseInt(req.getParameter("blogId"));
        JSONUtil.printByJSON(resp, Result.success(likeService.getLikeCount(blogId)));
    }
}
